package com.raymond.queue.handler;

import com.raymond.queue.common.msg.request.HeartbeatRequest;
import com.raymond.queue.common.msg.response.HeartbeatResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 心跳请求处理
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 14:24
 */
public class HeartbeatHandler extends SimpleChannelInboundHandler<HeartbeatRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HeartbeatRequest msg) throws Exception {
        System.out.println("收到心跳");
        ctx.writeAndFlush(new HeartbeatResponse());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("HeartbeatHandler Established connection with the remote client.");

        // do something

        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("HeartbeatHandler Disconnected with the remote client.");

        // do something

        ctx.fireChannelInactive();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

